/**   
 * @Title: DepartmentDao.java 
 * @package com.sysmanage.permissions.dao 
 * @Description: 
 * @author XXXX@163.com 
 * @date 2011-9-20 下午1:25:44 
 * @version V1.0   
 */

package com.sysmanage.permissions.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.sysmanage.framework.dao.hspring.BaseDAO;
import com.sysmanage.framework.dao.hspring.handler.IHandler;
import com.sysmanage.permissions.model.Department;

/** 
* @ClassName: DepartmentDao 
* @Description: 处理资源信息模块的DAO 
* @author XXXX@163.com 
* @date 2011-9-20 下午1:25:44 
*  
*/
@Repository(value="departmentDao")
public class DepartmentDao extends BaseDAO<Department> {

	/** 
	  * @Title: getMaxOrder 
	  * @Description:信息表中的排序最大值 
	  * @param @return
	  * @return int
	  * @throws 
	  */
	public int getMaxOrder(){
		IHandler handler = this.getHandler();
		Object result = handler.findObj("select max(orderId) from Department");
		if(result!=null){
			return (Integer)result;
		}else{
			return 0;
		}
		
	}
	
	
	
	/** 
	  * @Title: getTree 
	  * @Description: 获得公司树HTML代码 
	  * @param @param request
	  * @param @return
	  * @return String
	  * @throws 
	  */
	public List<Map<String,Object>> getTree(String id){
		List<Map<String,Object>> nodes = new ArrayList<Map<String,Object>>();
		//存放每层资源的临时变量
		List<Map<String,Object>> temp = new ArrayList<Map<String,Object>>();
		//第一级资源
		String hsql = "from Department where parentId is null";
		if(id!=null&&id.trim()!=null)
		{
			hsql = "from Department where parentId =" + id;
		}		
		List<Department> types  =  this.getHandler().findListOfObj(hsql);
		if(null == types || types.size() == 0){
			return nodes;
		}
		for(Department type: types){
				Map<String,Object> node = new HashMap<String,Object>();
				node.put("id",  type.getId()+"");
				node.put("text", type.getDeptName());
				nodes.add(node);
				temp.add(node);
		}
		//临时
		List<Map<String,Object>> doing = new ArrayList<Map<String,Object>>();
		doing.addAll(nodes);

		while(!doing.isEmpty()){
			temp = new ArrayList<Map<String,Object>>();
			for(Map<String,Object> item: doing){
				List<Object> children = new ArrayList<Object>();
				String pid = (String)item.get("id");
				List<Department> tempTypes  =  this.getHandler().findListOfObj("from Department where parentId=" + pid);
				for(Department type: tempTypes){
					Map<String,Object> node = new HashMap<String,Object>();
					node.put("id",  type.getId()+"");
					node.put("text", type.getDeptName());
					temp.add(node);
					children.add(node);
				}
				item.put("children", children.toArray(new Object[children.size()]));
			}
			doing = temp;
		}
		return nodes;
	}
	
	/** 
	  * @Title: getAll 
	  * @Description: 获得所有资源记录集合
	  * @param @return
	  * @return List<Resources>
	  * @throws 
	  */
	public List<Department> getAll(){
		return this.findList("from Department");
	}
}
